//
//  SecondRegisterViewController.m
//  Socrates
//
//  Created by shy on 15/3/14.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "SecondRegistViewController.h"
#import "LabelTextFieldTableViewCell.h"
#import "LabelChoiceViewCell.h"
#import "AppDelegate.h"
#import "CircleImage.h"

typedef enum {
    kRegister_portrait_item = 0,
    kRegister_Account_Items_Num,
} RegisterAccountItems;

typedef enum {
    kRegister_name_item,
    kRegister_school_item,
    kRegister_Name_Items_Num,
} RegisterNameItems;

typedef enum {
    kRegister_gender_item,
    kRegisrer_birth_item,
    kRegister_Gender_Items_Num,
} RegisterGenderItems;

typedef enum {
    kRegister_Interest_One = 0,
    kRegister_Interest_Two,
    kRegister_Interest_Three,
    kRegister_Interest_Num,
} RegisterInterestItems;

typedef enum {
    kRegister_Account_Section = 0 ,
    kRegister_Name_Section,
    kRegister_Gender_Section,
    kRegister_Interest_Section,
    kRegister_Section_Num,
} RegisterTableSectionItems;

static const int kTableViewPortraitTag = 501;
static const int kTableViewNameTag = 502;
static const int kTableViewSchoolTag = 503;
static const int kTableViewGenderTag = 504;
static const int kTableViewBirthTag = 505;

static const int kGenderMaleTag = 11;
static const int kGenderFemaleTag = 12;

static const int kTableViewHeaderHeightTop = 25;
static const int kTableViewHeaderHeightMidium = 40;


@interface SecondRegistViewController ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource> {
    UIScrollView *view_;
    UITableView *tableView_;
    UITextField *mobile_;
    UITextField *verifyCode_;
    UITextField *loginCode_;
    UITextField *loginVerify_;
    UIButton *next_;
    NSMutableDictionary *genderDic_;
    UIView *selectA_;
    UIView *selectB_;
    UIView *selectC_;
    UIButton *male_;
    UIButton *female_;
    UILabel *interestsHint_;
    UIButton *submit_;
    
}

@end

@implementation SecondRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    CGFloat tableviewHeight = 431;
    view_ = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    [view_ setScrollEnabled:YES];
    view_.delegate = self;
    [self.view addSubview:view_];
    
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, tableviewHeight)];
    tableView_.backgroundColor = [UIColor whiteColor];
    tableView_.dataSource = self;
    tableView_.delegate = self;
    tableView_.scrollEnabled = NO;
    [view_ addSubview:tableView_];
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, tableviewHeight, self.view.frame.size.width, 1.)];
    bottomLine.backgroundColor = [UIColor colorWithRed:224/255. green:224/255. blue:224/255. alpha:1.];
    [view_ addSubview:bottomLine];
    
    interestsHint_ = [[UILabel alloc] initWithFrame:CGRectMake(23, tableviewHeight + 3, self.view.frame.size.width - 46, 12)];
    interestsHint_.text = @"您至少需要选择一项。";
    interestsHint_.font = [UIFont systemFontOfSize:11.];
    interestsHint_.textAlignment = NSTextAlignmentLeft;
    interestsHint_.textColor = [UIColor grayColor];
    [view_ addSubview:interestsHint_];
    
    submit_ = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 160)/2, tableviewHeight + 25, 160, 40)];
    [submit_ setBackgroundColor:[UIColor colorWithRed:252/255. green:108/255. blue:108/255. alpha:1.]];
    [submit_ setTitle:@"注册" forState:UIControlStateNormal];
    [submit_ setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submit_ addTarget:self action:@selector(clickNext) forControlEvents:UIControlEventTouchUpInside];
    [view_ addSubview:submit_];
    
    UILabel *agreeRegister = [[UILabel alloc] initWithFrame:CGRectMake(0, tableviewHeight + 80, self.view.frame.size.width, 12)];
    agreeRegister.font = [UIFont systemFontOfSize:11.];
    agreeRegister.text = @"点击注册按钮表示您已同意";
    agreeRegister.textColor = [UIColor grayColor];
    agreeRegister.textAlignment = NSTextAlignmentCenter;
    [view_ addSubview:agreeRegister];
    
    UILabel *protocol = [[UILabel alloc] initWithFrame:CGRectMake(0, tableviewHeight + 105, self.view.frame.size.width, 12)];
    protocol.font = [UIFont systemFontOfSize:11.];
    protocol.textColor = [UIColor colorWithRed:252/255. green:108/255. blue:108/255. alpha:1.];
    protocol.text = @"识趣儿用户使用协议";
    protocol.textAlignment = NSTextAlignmentCenter;
    [view_ addSubview:protocol];
    
    UIView *protocolLine = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, tableviewHeight + 118, 100, 1.)];
    protocolLine.backgroundColor = [UIColor colorWithRed:252/255. green:108/255. blue:108/255. alpha:1.];
    [view_ addSubview:protocolLine];
}

- (void)configInterestSection {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    UIButton *selects1_ = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, 30, 30)];
    [selects1_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects2_ = [[UIButton alloc] initWithFrame:CGRectMake(110, 7, 30, 30)];
    [selects2_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects3_ = [[UIButton alloc] initWithFrame:CGRectMake(215, 7, 30, 30)];
    [selects3_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UILabel *interests1_ = [[UILabel alloc] initWithFrame:CGRectMake(40, 11, 60, 22)];
    interests1_.font = [UIFont systemFontOfSize:14.];
    interests1_.textColor = [UIColor grayColor];
    interests1_.text = @"美食旅游";
    UILabel *interests2_ = [[UILabel alloc] initWithFrame:CGRectMake(140, 11, 60, 22)];
    interests2_.textColor = [UIColor grayColor];
    interests2_.font = [UIFont systemFontOfSize:14.];
    interests2_.text = @"讲座沙龙";
    UILabel *interests3_ = [[UILabel alloc] initWithFrame:CGRectMake(245, 11, 60, 22)];
    interests3_.font = [UIFont systemFontOfSize:14.];
    interests3_.textColor = [UIColor grayColor];
    selectA_ = [[UIView alloc] initWithFrame:CGRectMake((width - 320)/2, 0, 320, 36)];
    [selectA_ addSubview:selects1_];
    [selectA_ addSubview:interests1_];
    [selectA_ addSubview:selects2_];
    [selectA_ addSubview:interests2_];
    [selectA_ addSubview:selects3_];
    [selectA_ addSubview:interests3_];
    
    UIButton *selects4_ = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, 30, 30)];
    [selects4_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects5_ = [[UIButton alloc] initWithFrame:CGRectMake(110, 7, 30, 30)];
    [selects5_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects6_ = [[UIButton alloc] initWithFrame:CGRectMake(215, 7, 30, 30)];
    [selects6_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UILabel *interests4_ = [[UILabel alloc] initWithFrame:CGRectMake(40, 11, 60, 22)];
    interests4_.font = [UIFont systemFontOfSize:14.];
    interests4_.textColor = [UIColor grayColor];
    UILabel *interests5_ = [[UILabel alloc] initWithFrame:CGRectMake(140, 11, 60, 22)];
    interests5_.font = [UIFont systemFontOfSize:14.];
    interests5_.textColor = [UIColor grayColor];
    UILabel *interests6_ = [[UILabel alloc] initWithFrame:CGRectMake(245, 11, 60, 22)];
    interests6_.font = [UIFont systemFontOfSize:14.];
    interests6_.textColor = [UIColor grayColor];
    selectB_ = [[UIView alloc] initWithFrame:CGRectMake((width - 320)/2, 0, 320, 36)];
    [selectB_ addSubview:selects4_];
    [selectB_ addSubview:interests4_];
    [selectB_ addSubview:selects5_];
    [selectB_ addSubview:interests5_];
    [selectB_ addSubview:selects6_];
    [selectB_ addSubview:interests6_];
    
    UIButton *selects7_ = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, 30, 30)];
    [selects7_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UIButton *selects8_ = [[UIButton alloc] initWithFrame:CGRectMake(110, 7, 30, 30)];
    [selects8_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    UILabel *interests7_ = [[UILabel alloc] initWithFrame:CGRectMake(40, 11, 60, 22)];
    interests7_.font = [UIFont systemFontOfSize:14.];
    interests7_.textColor = [UIColor grayColor];
    UILabel *interests8_ = [[UILabel alloc] initWithFrame:CGRectMake(140, 11, 60, 22)];
    interests8_.font = [UIFont systemFontOfSize:14.];
    interests8_.textColor = [UIColor grayColor];
    selectC_ = [[UIView alloc] initWithFrame:CGRectMake((width - 320)/2, 0, 320, 36)];
    [selectC_ addSubview:selects7_];
    [selectC_ addSubview:interests7_];
    [selectC_ addSubview:selects8_];
    [selectC_ addSubview:interests8_];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return kRegister_Section_Num;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    switch (section) {
        case kRegister_Account_Section:
            return kTableViewHeaderHeightTop;
        case kRegister_Name_Section:
            return kTableViewHeaderHeightTop;
        case kRegister_Gender_Section:
            return kTableViewHeaderHeightTop;
        case kRegister_Interest_Section:
            return kTableViewHeaderHeightMidium;
        default:
            break;
    }
    return 0;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 25)];
    view1.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 40)];
    UILabel *interests = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, width - 40, 40)];
    interests.text = @"选择你感兴趣的话题";
    interests.font = [UIFont systemFontOfSize:12.];
    interests.textColor = [UIColor grayColor];
    interests.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    [view2 addSubview:interests];
    view2.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    switch (section) {
        case kRegister_Account_Section:
            return view1;
        case kRegister_Name_Section:
            return view1;
        case kRegister_Gender_Section:
            return view1;
        case kRegister_Interest_Section:
            return view2;
        default:
            break;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case kRegister_Account_Section:
            return kRegister_Account_Items_Num;
        case kRegister_Name_Section:
            return kRegister_Name_Items_Num;
        case kRegister_Gender_Section:
            return kRegister_Gender_Items_Num;
        case kRegister_Interest_Section:
            return kRegister_Interest_Num;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch ([indexPath section]) {
        case kRegister_Account_Section:
            switch ([indexPath row]) {
                case kRegister_portrait_item:
                    return 65;
                    break;
                    
                default:
                    return 36;
                    break;
            }
        case kRegister_Name_Section:
            return 36;
        case kRegister_Gender_Section:
            return 36;
        case kRegister_Interest_Section:
            return 36;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIEdgeInsets edgeInset = tableView_.separatorInset;
    tableView_.separatorInset = UIEdgeInsetsMake(edgeInset.top, 23, edgeInset.bottom, 23);
    tableView_.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    NSString *cellIdentifier = @"RegisterCellIdentifier";
    UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    UIImageView *portrait = [[UIImageView alloc] initWithFrame:CGRectMake(width - 100, 3, 60, 60)];
    UIImage *userImage = [CircleImage circleImageWithName:@"angela_test" borderWidth:0 borderColor:[UIColor clearColor]];
    [portrait setImage:userImage];
    male_ = [[UIButton alloc] initWithFrame:CGRectMake(width - 150, 6, 24, 24)];
    [male_ setImage:[UIImage imageNamed:@"solid"] forState:UIControlStateSelected];
    [male_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    [male_ addTarget:self action:@selector(clickGender:) forControlEvents:UIControlEventTouchUpInside];
    male_.tag = kGenderMaleTag;
    UIImageView *male = [[UIImageView alloc] initWithFrame:CGRectMake(width - 120, 11, 13, 15)];
    [male setImage:[UIImage imageNamed:@"male"]];
    UIImageView *female = [[UIImageView alloc] initWithFrame:CGRectMake(width - 70, 11, 11, 15)];
    [female setImage:[UIImage imageNamed:@"female"]];
    female_ = [[UIButton alloc] initWithFrame:CGRectMake(width - 100, 6, 24, 24)];
    [female_ setImage:[UIImage imageNamed:@"solid"] forState:UIControlStateSelected];
    [female_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
    [female_ addTarget:self action:@selector(clickGender:) forControlEvents:UIControlEventTouchUpInside];
    female_.tag = kGenderFemaleTag;
    [genderDic_ setObject:male_ forKey:@"male"];
    [genderDic_ setObject:female_ forKey:@"female"];
    [self configInterestSection];
    switch ([indexPath section]) {
        case kRegister_Account_Section:
        {
            switch ([indexPath row]) {
                case kRegister_portrait_item:
                    cell.textLabel.text = @"头像";
                    [cell addSubview:portrait];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cell.tag = kTableViewPortraitTag;
                    break;
                default:
                    break;
            }
            
            break;
        }
           
        case kRegister_Name_Section: {
            switch ([indexPath row]) {
                case kRegister_name_item:
                    cell.textLabel.text = @"昵称";
                    cell.tag = kTableViewNameTag;
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case kRegister_school_item:
                    cell.tag = kTableViewSchoolTag;
                    cell.textLabel.text = @"学校";
                    cell.textLabel.font = [UIFont systemFontOfSize:16.];
                    cell.detailTextLabel.text = @"选择你所在的大学";
                    cell.detailTextLabel.textColor = [UIColor grayColor];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                default:
                    break;
            }
            break;
        }
            
        case kRegister_Gender_Section: {
            switch ([indexPath row]) {
                case kRegister_gender_item:
                    cell.textLabel.text = @"性别";
                    cell.tag = kTableViewGenderTag;
                    [cell addSubview:male_];
                    [cell addSubview:male];
                    [cell addSubview:female_];
                    [cell addSubview:female];
                    break;
                case kRegisrer_birth_item:
                    cell.tag = kTableViewBirthTag;
                    cell.textLabel.text = @"出生日期";
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                default:
                    break;
            }
            break;
        }
        case kRegister_Interest_Section:
        {
            switch ([indexPath row]) {
                case kRegister_Interest_One:
                    [cell addSubview:selectA_];
                    break;
                case kRegister_Interest_Two:
                    [cell addSubview:selectB_];
                    break;
                case kRegister_Interest_Three:
                    [cell addSubview:selectC_];
                    break;
            }
            break;
        }
    }
    
    return cell;
}

- (void)clickNext {
    NSLog(@"click next.");
}

- (void)clickGender: (id)sender{
    if (male_.selected == YES) {
        male_.selected = NO;
        female_.selected = YES;
        [male_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
        [female_ setImage:[UIImage imageNamed:@"solid"] forState:UIControlStateNormal];
        [tableView_ reloadData];
    } else if (male_.selected == NO) {
        male_.selected = YES;
        female_.selected = NO;
        [male_ setImage:[UIImage imageNamed:@"solid"] forState:UIControlStateNormal];
        [female_ setImage:[UIImage imageNamed:@"hollow"] forState:UIControlStateNormal];
        [tableView_ reloadData];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView_ deselectRowAtIndexPath:indexPath animated:NO];
    switch ([tableView cellForRowAtIndexPath:indexPath].tag) {
        case kTableViewPortraitTag:
            break;
            
        case kTableViewNameTag: {
            break;
        }
        case kTableViewSchoolTag: {
            
        }
        case kTableViewGenderTag: {
            
        }
        case kTableViewBirthTag: {
            
        }
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [view_ setContentSize:CGSizeMake(320,550)];
}
@end